﻿using BLL.Interface;
using Common.Extensions;
using DAL.Interface;
using Microsoft.EntityFrameworkCore;
using Model;
using Model.DTO.Member;
using Model.DTO.Public.GetList;
using Model.DTO.Public.OperateId;
using Model.Entity.Type;

namespace BLL
{
    /// <summary>
    /// 会员类型业务逻辑层接口
    /// </summary>
    public class MemberTypeBLL : IMemberTypeBLL
    {
        private readonly IBaseDAL<TB_MemberType> _memberTypeDAL;
        public MemberTypeBLL(IBaseDAL<TB_MemberType> memberTypeDAL)
        {
            _memberTypeDAL = memberTypeDAL;
        }
        /// <summary>
        /// 添加会员类型
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> AddMemberType(AddMemberTypeDTO param)
        {
            try
            {
                #region 过滤
                var memberTypes = await _memberTypeDAL.GetEntityAllAsync();
                var existName = await memberTypes.Where(x => x.Name == param.Name).AnyAsync();
                if (existName)
                {
                    return ResponseMessage.CreateError("名称已存在");
                }
                var existCode = await memberTypes.Where(x => x.Code == param.Code).AnyAsync();
                if (existCode)
                {
                    return ResponseMessage.CreateError("代码已存在");
                }
                #endregion
                var memberType = new TB_MemberType();
                param.CopyPropertiesTo(memberType);
                var result = await _memberTypeDAL.AddEntityAsync(memberType);
                return result ? ResponseMessage.CreateSuccess("添加成功") : ResponseMessage.CreateError("添加失败");
            }
            catch
            {
                return ResponseMessage.CreateError("添加失败");
            }
        }
        /// <summary>
        /// 删除会员类型
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> DeleteMemberType(OperateIdDTO_INT param)
        {
            try
            {
                if (param.Id > 0)
                {
                    param.Ids.Clear();
                    param.Ids.Add(param.Id);
                    param.Id = 0;
                    return await DeleteMemberType(param);
                }
                #region 过滤
                if (param.Ids.Count <= 0)
                {
                    return ResponseMessage.CreateError("Id无效");
                }
                #endregion
                using var tran = await _memberTypeDAL.BeginTransactionAsync();
                try
                {
                    foreach (var id in param.Ids)
                    {
                        await _memberTypeDAL.DeleteEntityAsync(id);
                    }
                    await tran.CommitAsync();
                    return ResponseMessage.CreateSuccess("删除成功");
                }
                catch
                {
                    await tran.RollbackAsync();
                    throw;
                }
            }
            catch
            {
                return ResponseMessage.CreateError("删除失败");
            }
        }
        /// <summary>
        /// 编辑会员类型
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> EditMemberType(EditMemberTypeDTO param)
        {
            try
            {
                #region 过滤
                var memberType = await _memberTypeDAL.GetEntityAllAsync(x => x.Id == param.Id).Result.FirstOrDefaultAsync();
                if (memberType == null)
                {
                    return ResponseMessage.CreateError("会员类型不存在");
                }
                var elseArticleTypes = await _memberTypeDAL.GetEntityAllAsync(x => x.Id != param.Id);
                var existName = await elseArticleTypes.Where(x => x.Name == param.Name).AnyAsync();
                if (existName)
                {
                    return ResponseMessage.CreateError("名称已存在");
                }
                var existCode = await elseArticleTypes.Where(x => x.Code == param.Code).AnyAsync();
                if (existCode)
                {
                    return ResponseMessage.CreateError("代码已存在");
                }
                #endregion
                param.CopyPropertiesTo(memberType);
                var result = await _memberTypeDAL.EditEntityAsync(memberType);
                return result ? ResponseMessage.CreateSuccess("编辑成功") : ResponseMessage.CreateError("编辑失败");
            }
            catch
            {
                return ResponseMessage.CreateError("编辑失败");
            }
        }
        /// <summary>
        /// 获取会员类型列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ResponseMessage> GetMemberTypeList(GetListDTO param)
        {
            try
            {
                #region 参数校验
                if (param.Page <= 0)
                {
                    param.Page = 1;
                }
                if (param.Size <= 0)
                {
                    param.Size = 10;
                }
                #endregion
                var memberTypes = await _memberTypeDAL.GetEntityAllAsync(x => x.Name.Contains(param.Query ?? string.Empty));
                int total = await memberTypes.CountAsync();
                var list = await memberTypes.Skip((param.Page - 1) * param.Size).Take(param.Size).ToListAsync();
                return ResponseMessage.CreateSuccess("获取成功", new { total, list });
            }
            catch
            {
                return ResponseMessage.CreateError("获取失败");
            }
        }
    }
}
